package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.base.App;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.BorrowMoneyRecordPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.BorrowMoneyRecordBean;
import com.sskj.lib.bean.FrashBean;
import com.sskj.lib.util.CommonUtil;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.FUNCTIONS_ROLL_RECORD)
public class BorrowMoneyRecordActivity extends BaseActivity<BorrowMoneyRecordPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @Autowired
    String type;
    @BindView(R2.id.roll_out_record_rv)
    RecyclerView rollOutRecordRv;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    private int page = 1;
    private int size = 20;
    private BorrowMoneyRecordBean data;
    private BaseAdapter<BorrowMoneyRecordBean.ListBean> adapter;
    private BaseAdapter<FrashBean.ListBean> myadapter;
    private String state = App.INSTANCE.getString(R.string.functions_borrowMoneyRecordActivity1);

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_roll_ou_trecord;
    }

    @Override
    public BorrowMoneyRecordPresenter getPresenter() {
        return new BorrowMoneyRecordPresenter();
    }

    @Override
    public void initView() {

        ARouter.getInstance().inject(this);
        if (type.equals(Constans.JBSX)) {
            tvTitle.setText(App.INSTANCE.getString(R.string.functions_borrowMoneyRecordActivity2));
            setCoinType();
            getJbsxData();
        }
        if (type.equals(Constans.CLTZ)) {
            tvTitle.setText(App.INSTANCE.getString(R.string.functions_borrowMoneyRecordActivity2));
            setCoinType();
            getCltzData();
        }
        if (type.equals(Constans.SHANDUI)) {
            tvTitle.setText(App.INSTANCE.getString(R.string.functions_borrowMoneyRecordActivity3));
            setFrashData();
            getFrash();
        }
    }

    //策略投资记录
    private void getCltzData() {
        mPresenter.getJbsxList("1", page + "", size + "", true);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getJbsxList("1", page + "", size + "", false);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getJbsxList("1", page + "", size + "", false);
        });
    }

    //闪兑记录
    private void getFrash() {
        mPresenter.getFrashList(page, size, true);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getFrashList(page, size, false);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getFrashList(page, size, false);
        });
    }

    //借币生息记录
    private void getJbsxData() {
        mPresenter.getJbsxList("2", page + "", size + "", true);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getJbsxList("2", page + "", size + "", false);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getJbsxList("2", page + "", size + "", false);
        });
    }

    @Override
    public void initData() {
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BorrowMoneyRecordActivity.class);
        context.startActivity(intent);
    }

    @SuppressLint("CheckResult")
    public void getJbsxListSuccess(BorrowMoneyRecordBean data) {
        if (adapter != null) {
            if (page == 1) {
                adapter.setNewData(data.getList());
            } else {
                adapter.addData(data.getList());
            }
        }
    }

    @SuppressLint("CheckResult")
    public void getRrashListSuccess(FrashBean data) {
        if (myadapter != null) {
            if (page == 1) {
                myadapter.setNewData(data.getList());
            } else {
                myadapter.addData(data.getList());
            }
        }
    }

    private void setCoinType() {
        rollOutRecordRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<BorrowMoneyRecordBean.ListBean>(R.layout.borrow_money_record_item, null, rollOutRecordRv, true) {
            @Override
            public void bind(ViewHolder holder, BorrowMoneyRecordBean.ListBean item) {
                if (item.getStatus().equals("1")) {
                    state = App.INSTANCE.getString(R.string.functions_borrowMoneyRecordActivity4);
                } else if (item.getStatus().equals("2")) {
                    state = App.INSTANCE.getString(R.string.functions_borrowMoneyRecordActivity5);
                }
                holder.setText(R.id.borrow_title_tv_item, item.getCommodity_name())
                        .setText(R.id.borrow_state_tv_item, state)
                        .setText(R.id.borrow_money_tv_item, CommonUtil.dealCoinNum(item.getCommodity_price(), item.getPid()) + item.getPid())
                        .setText(R.id.borrow_end_time_tv_item, TimeFormatUtil.formatE(Long.valueOf(item.getMaturity_time()) * 1000))
                        .setText(R.id.borrow_get_money_tv_item, CommonUtil.dealCoinNum(item.getYuqishouru(), item.getPid()) + item.getPid())
                        .setText(R.id.tv_year_income_percent, NumberUtil.keep2(item.getInterest_rate()) + "%")
                        .setText(R.id.borrow_day_tv_item, item.getCycle() + App.INSTANCE.getString(R.string.functions_strategyActivity3))
                        .setText(R.id.borrow_start_time_tv_item, TimeFormatUtil.formatE(Long.valueOf(item.getEntry_into_force_time()) * 1000));
            }
        };
        ItemClickSupport.addTo(rollOutRecordRv).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                if (adapter.getData().size() == 0) {
                    return;
                }
                ARouter.getInstance().build(RConfig.FUNCTION_INCOME_DETAIL)
                        .withString(Constans.ORDER_ID, adapter.getData().get(position).getId())
                        .navigation();
            }
        });
    }

    private void setFrashData() {
        rollOutRecordRv.setLayoutManager(new LinearLayoutManager(this));
        myadapter = new BaseAdapter<FrashBean.ListBean>(R.layout.frash_item_layout, null, rollOutRecordRv, true) {
            @Override
            public void bind(ViewHolder holder, FrashBean.ListBean item) {
                holder.setText(R.id.frash_name_tv_item, item.getTitle())
                        .setText(R.id.frash_time_tv_item, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.frash_number_tv_item, item.getNanmer())
                        .setText(R.id.frash_money_tv_item, item.getTot_old_num());
            }
        };
    }

    public void onStopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }
}
